package com.itheima.controller;

import com.itheima.pojo.AddressBook;
import com.itheima.service.AddressBookService;
import com.itheima.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 放学后海堤日记
 * @Date: 2023/4/18 17:16
 * @Desc:
 */
@RestController
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 查看用户地址列表
     *
     * @return
     */
    @GetMapping("/addressBook/list")
    public R<List<AddressBook>> listAll() {
        List<AddressBook> addressBooks = addressBookService.listAll();

        return R.success(addressBooks);
    }

    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/addressBook/default")
    public R<String> setDefault(@RequestBody AddressBook addressBook) {
        addressBookService.setDefault(addressBook);

        return R.success("设置成功");
    }

    /**
     * 根据id查询地址信息
     *
     * @param id
     * @return
     */
    @GetMapping("/addressBook/{id}")
    public R<AddressBook> findById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.findById(id);

        return R.success(addressBook);
    }

    /**
     * 修改地址信息
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/addressBook")
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.updateAddressBook(addressBook);

        return R.success("修改成功");
    }

    /**
     * 新增地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping("/addressBook")
    public R<String> addAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.addAddressBook(addressBook);

        return R.success("添加成功");
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping("/addressBook")
    public R<String> deleteaddAddressBook(Long ids) {
        addressBookService.deleteaddAddressBook(ids);

        return R.success("删除成功");
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/addressBook/default")
    public R<AddressBook> findDefaultAddressBook() {
        AddressBook defaultAddressBook = addressBookService.findDefaultAddressBook();

        return R.success(defaultAddressBook);
    }
}
